/* let p = new Promise((resolve, reject) => {
  setTimeout(() => {
    console.log('aqie')
    resolve()
  }, 1000)
}).then(() => {
  console.log('success')
}, () => {
  console.log('fail')
}) */


function ajax(url,sucessCallback, failCallback) {
  var xmlhttp
  if(window.XMLHttpRequest){
    xmlhttp = new XMLHttpRequest()
   } else {
  
   }
  
   // 2. 发送请求
   xmlhttp.open('GET', url, true)
   xmlhttp.send()
  
   // 3. 服务端响应
   xmlhttp.onreadystatechange = function(){
     if(xmlhttp.readyState === 4 && xmlhttp.status === 200) {
       var obj = JSON.parse(xmlhttp.responseText)
       sucessCallback && sucessCallback(obj)
     } else if(xmlhttp.readyState === 4 && xmlhttp.status === 404){
      failCallback && failCallback(xmlhttp.statusText)
     }
   }
 }

 /* new Promise((resolve, reject) => {
   ajax('static/a.json', res => {
     console.log(res)
     resolve()
   })
 }).then(() => {
  console.log('a success')
  return new Promise((resolve, reject) => {
      ajax('static/b.json', res => {
        console.log(res)
        resolve()
      })
    })
}).then(res => {
  console.log('b success')
}) */


function getPromise(url) {
  return new Promise((resolve, reject)=>{
    ajax(url, res => {
      resolve(res)
    }, err => {
      reject(err)
    })
  })
}

// 异步操作统一处理
getPromise('static/as.json').then(res => {
  console.log(res)
  return getPromise('static/b.json')
}/* , err => {
  console.log(err)
} */).then(res => {
  console.log(res)
  return getPromise('static/c.json')
}).then(res => {
  console.log(res)
}).catch(err => {
  console.log(err)
})